# encoding=utf-8
import requests
from sdktest_module.utils.constant import *


# 测试用例结果上报需要的参数
case_params = {
    "appid": 0,  # appid
    "createBy": "string",  # 创建人
    "createTime": "2020-06-09T16:37:13.663Z",  # 创建时间
    "endTime": "2020-06-09T16:37:13.663Z",  # 结束时间
    "id": 0,  # id 自增
    "info": "",  # 手机信息
    "logPath": "string",  # 日志下载链接
    "scriptContent": "string",  # 脚本内容
    "status": 0,  # 测试结果 0 -> 失败，1 ->成功
    "testcaseName": "string",  # 测试用例英文名
    "testcaseNameCn": "string",  # 测试用例中文名
    "sdkVer": ""  # sdk版本
}

def report_datacube(case_result, is_debug:True):
    if case_result != 'pass':
        case_params['status'] = 0
    else:
        case_params['status'] = 1

    if not is_debug:
        # 数据上报
        url = 'http://datacube.yy.com/apm-admin/proxy/autoTest/apmAutotestResult/add'
        result = requests.post(url, json=case_params)
        try:  # 数据上报返回可能为None
            print('数据上报结果: %s' % result.json())
        except:
            print('数据上报返回结果错误:[result=' + str(result) + ']')

def uploadCaseInfos(request, user_list, taskId):
    # 上传用例执行的信息包括task_id、用例文件名、设备信息[品牌、型号、序列号、系统版本、使用者]
    deviceInfo = []
    for user in user_list:
        info = {}
        info.setdefault("user", user.driver.role)
        info.setdefault("brand", user.driver.device.brand)
        info.setdefault("model", user.driver.device.model)
        info.setdefault("serial", user.driver.device.serial)
        info.setdefault("osVersion", user.platform + user.driver.device.os_version)
        deviceInfo.append(info)

    case_info = {}
    case_info.setdefault("taskId", taskId)
    case_info.setdefault("fileName", request.fspath.basename)
    case_info.setdefault("caseName", request.function.case_func_name)
    case_info.setdefault("deviceInfo", deviceInfo)
    print("用例执行信息:" + str(case_info).replace('\'', '"'))
    url = 'http://automated.yy.com/sdk_test/add_device_infos'
    resp = requests.post(url=url, json=case_info)
    print("用例执行信息上报结果:" + resp.content.decode('utf-8'))

